package com.situ.staff.department.service.Impl;

import java.sql.SQLException;
import java.util.List;

import com.situ.staff.department.dao.DepartmentDao;
import com.situ.staff.department.model.DepartmentModel;
import com.situ.staff.department.service.IDepartmentService;

public class DepartmentServiceImpl implements IDepartmentService {
	DepartmentDao dao = new DepartmentDao();
	@Override
	public String insert(DepartmentModel model) throws SQLException {
		DepartmentModel m0 = new DepartmentModel(model.getCode());
		DepartmentModel m1 = dao.selectModel(m0);
		if(m1==null) {
			Integer res = dao.insert(model);
			return res==null?null:res.toString();
		}else {
			return "exist";
		}
	}

	@Override
	public String delete(DepartmentModel model) throws SQLException {
		
		return dao.delete(model)+"";
	}

	@Override
	public String update(DepartmentModel model) throws SQLException {
		
		return dao.updateActive(model)+"";
	}

	@Override
	public List<DepartmentModel> selectList(DepartmentModel model) throws SQLException {
		String code = model.getCode();
		model.setCode(code == null ? "%%" : "%" + code + "%");
		String name = model.getName();
		model.setName(name == null ? "%%" : "%" + name + "%");
		
		return dao.selectList(model);
	}

	@Override
	public DepartmentModel selectModel(DepartmentModel model) throws SQLException {

		return dao.selectModel(model);
	}

	@Override
	public Integer selectCount(DepartmentModel model) throws SQLException {
		DepartmentModel m0 = new DepartmentModel();
		String code = model.getCode();
		m0.setCode(code == null ? "%%" : "%" + code + "%");
		String name = model.getName();
		m0.setName(name == null ? "%%" : "%" + name + "%");
		
		return dao.selectCount(m0);
	}

}
